import { FieldPacket, RowDataPacket } from "mysql2";
import { pool } from "./000_pool";

pool.query<RowDataPacket[]>('select * from user')
    .then(([results, fields]) => {
        console.log('----------------------------1');
        for (const result of results) {
            console.log(result);
        }

        for (const field of fields) {
            console.log(field, field.name);
        }
    })


pool.query<RowDataPacket[]>({ sql: 'select * from user', rowsAsArray: true })
    .then(([results, fields]) => {
        console.log('----------------------------2');
        for (const result of results) {
            console.log(result);
        }

        for (const field of fields) {
            console.log(field, field.name);
        }
    })

pool.query<RowDataPacket[][]>({ sql: `select * from user;select * from user;`, rowsAsArray: true })
    .then(([results, fields]) => {
        console.log('----------------------------3');
        for (const result of results) {
            console.log(result);
        }

        for (const field of fields) {
            console.log(field, field.name);
        }
    })

pool.execute<RowDataPacket[]>({ sql: 'select * from user where age>?', values: [19], rowsAsArray: true })
    .then(([results, fields]) => {
        console.log('----------------------------4');
        for (const result of results) {
            console.log(result);
        }

        for (const field of fields) {
            console.log(field, field.name);
        }
    })

interface User extends RowDataPacket {
    name: string,
    age: number
}

pool.query<User[]>({ sql: 'select * from user' })
    .then(([results, fields]) => {
        console.log('----------------------------5');
        for (const result of results) {
            console.log(result);
        }

        for (const field of fields) {
            console.log(field, field.name);
        }
    })


setTimeout(() => {
    pool.end()
}, 2000);

